package com.thprojects.myphotomemories.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.thprojects.myphotomemories.R;
import com.thprojects.myphotomemories.controller.DbController;
import com.thprojects.myphotomemories.controller.FSController;
import com.thprojects.myphotomemories.controller.GuiController;
import com.thprojects.myphotomemories.model.Contact;
import com.thprojects.myphotomemories.model.Photo;

import exception.PhotoSourceNotFoundException;
import exception.SDNotReadyException;

public class PhotoView extends Activity{
	
	FSController mFSController;
	Photo mPhoto;
	DbController mDbController;
	
	LinearLayout mOverlayInfo;
	TextView mName, mCaption, mAlbum, mLocation, mContacts;
	ImageView mImage;

	
    //cancel new album button
    /* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mFSController = FSController.getFSController(this);
		
		//fill screen
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		mDbController = DbController.getDbController(this);
		
		setContentView(R.layout.photo_view);
		
		Intent i = getIntent();
        mPhoto = (Photo) i.getParcelableExtra("photo");
        
        mImage = (ImageView)findViewById(R.id.photo_cont); 
        mOverlayInfo = (LinearLayout)findViewById(R.id.overlay_info);
        mName = (TextView)findViewById(R.id.overlay_info_name);
        mCaption = (TextView)findViewById(R.id.overlay_info_caption);
        mAlbum = (TextView)findViewById(R.id.overlay_info_album);
        mLocation = (TextView)findViewById(R.id.overlay_info_location);
        mContacts = (TextView)findViewById(R.id.overlay_info_contacts);
        
        mImage.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				showOverlayInfo();
			}
		});
        
        hideOverlayInfo();
       
        init();
	}
	
	private void init(){
		mPhoto = mDbController.getPhotoFromId(mPhoto.getId());
		mName.setText(mPhoto.getName());
        mCaption.setText(mPhoto.getCaption());
        mAlbum.setText("Album: "+mPhoto.getAlbum().getName());
        mLocation.setText("Location: "+mPhoto.getLocation());
        if (mPhoto.getContacts().size() == 0){
        	mContacts.setText("No contacts tagged in this photo");
        }
        else{
        	for (Contact c: mPhoto.getContacts()){
        		Log.d("MPM", c.getDisplayName());
        	}
        	mContacts.setText("with: ");
        	for (int c=0; c<mPhoto.getContacts().size()-1; c++){
        		mContacts.append(mPhoto.getContacts().get(c).getDisplayName()+", ");
        	}
        	mContacts.append(mPhoto.getContacts().get(mPhoto.getContacts().size()-1).getDisplayName());
        }
        
        
        Display d = getWindowManager().getDefaultDisplay();
        try {
			mImage.setImageBitmap(mFSController.decodeSampledBitmap(mPhoto.getPath(), d.getWidth(), d.getHeight()));
		} catch (PhotoSourceNotFoundException e) {
			GuiController.showError(this, e.getMessage());
		} catch (SDNotReadyException e) {
			GuiController.showError(this, e.getMessage());
		}
	}

	public void onRestart(){
		super.onRestart();
		init();
	}
	
	private void showOverlayInfo(){
		mOverlayInfo.setVisibility(View.VISIBLE);
		hideOverlayInfo();
	}
	
	private void hideOverlayInfo(){
		Animation a = AnimationUtils.loadAnimation(this, R.anim.hide);
		mOverlayInfo.startAnimation(a);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		
		MenuItem rotate = menu.add("Rotate");
		rotate.setIcon(R.drawable.menu_rotate);
		rotate.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				rotate();
				return false;
			}
		});
		
		MenuItem facebook = menu.add("Share on Facebook");
		facebook.setIcon(R.drawable.menu_fb);
		facebook.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				shareOnFacebook();
				return false;
			}
		});
		
		MenuItem contacts = menu.add("Add effect");
		contacts.setIcon(R.drawable.menu_effect_image);
		contacts.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				addEffectToImage();
				return false;
			}
		});
		
		
		MenuItem editImage = menu.add("Draw on image");
		editImage.setIcon(R.drawable.menu_draw_image);
		editImage.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				drawOnImage();
				return false;
			}
		});
		
		MenuItem editDetails = menu.add("Edit details");
		editDetails.setIcon(R.drawable.menu_edit_details);
		editDetails.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				editDetails();
				return false;
			}
		});
		
		MenuItem delete = menu.add("Remove from album");
		delete.setIcon(R.drawable.menu_delete);
		delete.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				removeFromAlbum();
				return false;
			}
		});
		
		
		
		
		return true;
	}

	
	private void rotate(){
		GuiController.showRotate(this, mPhoto);	
	}
	
	private void shareOnFacebook(){
		GuiController.showShareFb(this, mPhoto);	
	}
	
	private void drawOnImage(){
		GuiController.showDrawPhoto(this, mPhoto);	
	}
	
	private void addEffectToImage(){
		GuiController.showEffectsPhoto(this, mPhoto);
	}
	
	private void editDetails(){
		GuiController.editPhotoDetails(this, mPhoto);
	}

	private void removeFromAlbum(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Are you sure?")
			   .setMessage("Are you sure you want to remove this photo from the album?")
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	    mDbController.removePhoto(mPhoto);
		        	    /*
		        	    try {
							mFSController.deleteExternalStoragePrivatePicture(mPhoto.getPath());
						} catch (SDNotReadyException e) {
							GuiController.showError(PhotoView.this, "Could not delete the image from your file system");
						}
						*/
		        	    PhotoView.this.finish();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		builder.create().show();
	}
	
}

